Practical-11
Mobile Application
Practical List
Create an android program to perform all database operations like insert, update, delete, and display.
Steps
- Create a new Android project in Android Studio.
- Open the
activity_main.xmllayout file. - Add a
Buttonelement to the layout with the following attributes:android:id="@+id/insertButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Insert"android:layout_gravity="center"
- Add a
Buttonelement to the layout with the following attributes:android:id="@+id/updateButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Update"android:layout_gravity="center"
- Add a
Buttonelement to the layout with the following attributes:android:id="@+id/deleteButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Delete"android:layout_gravity="center"
- Add a
Buttonelement to the layout with the following attributes:android:id="@+id/displayButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Display"android:layout_gravity="center"
- Open the
MainActivity.javafile. - Inside the
onCreatemethod, add the following code to set the content view to theactivity_main.xmllayout:setContentView(R.layout.activity_main); - Create a new method called
insertDatato insert data into the database:private void insertData() {
// Code to insert data into the database
} - Create a new method called
updateDatato update data in the database:
private void updateData() {
// Code to update data in the database
}
- Create a new method called
deleteDatato delete data from the database:
private void deleteData() {
// Code to delete data from the database
}
- Create a new method called
displayDatato display data from the database:
private void displayData() {
// Code to display data from the database
}
- Add click listeners to the buttons in the
onCreatemethod to call the respective methods:
Button insertButton = findViewById(R.id.insertButton);
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertData();
}
});
Button updateButton = findViewById(R.id.updateButton);
updateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateData();
}
});
Button deleteButton = findViewById(R.id.deleteButton);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteData();
}
});
Button displayButton = findViewById(R.id.displayButton);
displayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayData();
}
});
- Implement the database operations in the respective methods using SQLiteOpenHelper or any other database library of your choice.